package com.chen.blue.service.manager.factory;

import com.chen.blue.base.FileBase;
import com.chen.blue.converter.business.ResourceConverter;
import com.chen.blue.dictionary.CommonDict;
import com.chen.blue.domain.dto.business.ResourceDto;
import com.chen.blue.domain.dto.system.LogDto;
import com.chen.blue.domain.model.business.ContestApplyActiveModel;
import com.chen.blue.domain.model.business.ContestApplyRecordModel;
import com.chen.blue.domain.model.business.ResourceModel;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.service.business.IResourceService;
import com.chen.blue.service.business.impl.IResourceServiceImpl;
import com.chen.blue.service.candidate.CandidateInfoService;
import com.chen.blue.service.system.ILogService;
import com.chen.blue.service.system.impl.ILogServiceImpl;
import com.chen.blue.utils.MinioUtil;
import com.chen.blue.utils.SpringUtil;
import com.chen.blue.utils.ip.AddressUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Objects;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 * @author ruoyi
 */
@Slf4j
public class AsyncFactory {

    private static final ILogService logService = SpringUtil.getBean(ILogServiceImpl.class);

    private static final IResourceService iResourceService = SpringUtil.getBean(IResourceServiceImpl.class);

    /**
     * 操作日志记录
     *
     * @param logDto 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final LogDto logDto) {
        return new TimerTask() {
            @Override
            public void run() {
                // 远程查询操作地点
                logDto.setLogOptionAddressWhere(AddressUtils.getRealAddressByIP(logDto.getLogOptionAddressIp()));
                try {
                    logService.addLog(logDto);
                } catch (DtoNullException e) {
                    e.printStackTrace();
                }
            }
        };
    }


    /**
     * 异步关联资源
     *
     * @param fileBase
     * @param contentType
     * @param inputStream
     * @param contestApplyActiveModel
     * @param contestApplyRecordModel
     * @return
     */
    public static TimerTask fileUploadAsync(FileBase fileBase, String contentType, Integer optionType, InputStream inputStream, ContestApplyActiveModel contestApplyActiveModel, ContestApplyRecordModel contestApplyRecordModel) {

        return new TimerTask() {
            @Override
            public void run() {

                // 获取相关资源
                ResourceDto resourceDto = null;
                if (fileBase != null) {

                    resourceDto = new ResourceDto();
                    FileBase fileInfo = MinioUtil.upload(fileBase, inputStream, contentType);
                    BeanUtils.copyProperties(fileInfo, resourceDto);
                    CandidateInfoService.setCreator(resourceDto);
                    resourceDto.setActiveId(contestApplyActiveModel.getId());

                }

                // 记录表关联资源
                if (CommonDict.OPTION_TYPE_REPORT.equals(optionType) && Objects.nonNull(contestApplyRecordModel) && (resourceDto != null)) {

                    resourceDto.setRecordId(contestApplyRecordModel.getId());

                }

                ResourceModel resourceModel = ResourceConverter.dtoToModel(resourceDto);
                CandidateInfoService.setCreator(resourceModel);

                if (!iResourceService.save(resourceModel))
                    log.warn("资源保存失败");

            }
        };
    }
}
